package com.gt.acc

import com.gt.SCUtil
import org.apache.spark.SparkContext
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD

object Spark_bc_01 {

  def main(args: Array[String]): Unit = {
    val sc: SparkContext = SCUtil.createLocalSc()
    val rdd: RDD[String] = sc.makeRDD(List("hello", "spark", "scala", "spark", "scala"))

    val map: Map[String, Int] = Map("hello" -> 1, "spark" -> 2, "scala" -> 3)
    val bc: Broadcast[Map[String, Int]] = sc.broadcast(map)

    val rdd2: RDD[(String, Int)] = rdd.map(str => {
      val value = bc.value.getOrElse(str, 0)
      (str, value)
    })

    rdd2.foreach(println)

    sc.stop()

  }

}
